package student.design_pattern.observer_pattern;

import cn.hutool.core.lang.Console;

import java.util.ArrayList;
import java.util.List;

public class Subject {

    // 保存注册的观察者对象
    private final List<Observer> mObervers = new ArrayList<>();

    // 注册观察者对象
    public void attach(Observer observer) {
        mObervers.add(observer);
        Console.log("SZH", "attach an observer");
    }

    // 注销观察者对象
    public void detach(Observer observer) {
        mObervers.remove(observer);
        Console.log("SZH", "detach an observer");
    }

    //通知所有注册的观察者对象
    public void notifyEveryOne(String newState) {
        for (Observer observer : mObervers) {
            observer.update(newState);
        }
    }
}
